package com.tianzeculture.stagebao.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.ArtistActivity;
import com.tianzeculture.stagebao.activity.home.adapter.TypeDetailAdapter;
import com.tianzeculture.stagebao.activity.home.popwindow.SelectCityPopWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.SelectPriceWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.SelectTypeWindow;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.AllIndustryBean;
import com.tianzeculture.stagebao.bean.Notice;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.TypeDetailListBean;
import com.tianzeculture.stagebao.net.CommonRestUsage;
import com.tianzeculture.stagebao.net.HomeRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.L;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.popwindow.PopWindowSelectAdapter;
import com.tianzeculture.stagebao.popwindow.SelectPopWindow;
import com.tianzeculture.stagebao.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.tianzeculture.stagebao.util.Util.PAGE_SIZE;

/**
 * Created by Administrator on 2018/8/27.
 */

public class StyleSelectActivity extends BaseActivity implements SelectPopWindow.SelectWindowLitenner, SelectCityPopWindow.SelectCityListner, SelectPriceWindow.SelectWindowLitenner, SelectTypeWindow.SelectWindowLitenner {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.mRecyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.edtSearch)
    EditText edtSearch;
    @BindView(R.id.boxCity)
    CheckBox boxCity;
    @BindView(R.id.boxSex)
    CheckBox boxSex;
    @BindView(R.id.boxOut)
    CheckBox boxOut;
    @BindView(R.id.boxStyle)
    CheckBox boxStyle;

    String cityName = "";

    @Request
    HomeRestUsage homeRestUsage = null;

    private int typeDetailId = 99;
    String searchString = "";
    String price = "";
    String sex = "2";
    int pager = 1;
    TypeDetailAdapter adapter;
    List<TypeDetailListBean.TypeDetailBean> list = new ArrayList<>();

    String style;
    String getStyle;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_select_style;
    }

    @Request
    CommonRestUsage commonRestUsage = null;
    private int industryListId = 5;
    List<AllIndustryBean> allJobList;
    AllIndustryBean bean;

    @Override
    public void initUI() {
        super.initUI();
        getStyle = getIntent().getStringExtra("style");
        style = getStyle;
        if (null != style) {
            if ("其他".equals(style)) {
                boxStyle.setEnabled(false);
                boxStyle.setText("其他");
            } else boxStyle.setEnabled(true);
            commonRestUsage.industryList(industryListId);
        }
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(manager);
        adapter = new TypeDetailAdapter(R.layout.item_type_detail, list, this);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(StyleSelectActivity.this, ArtistActivity.class).putExtra("otherId", list.get(position).getUserId()));
            }
        });
        mRecyclerview.setAdapter(adapter);
        edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    searchString = edtSearch.getText().toString().trim();
                    showProgressDialog();
                }
                return false;
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pager = 1;
                        refreshlayout.finishRefresh();
                        homeRestUsage.getTypeDetail(typeDetailId, searchString, style, cityName, sex, price, pager, Util.PAGE_SIZE);
//                        refreshlayout.setNoMoreData(false);
                    }
                }, 2000);

            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pager++;
                        homeRestUsage.getTypeDetail(typeDetailId, searchString, style, cityName, sex, price, pager, Util.PAGE_SIZE);
                        refreshLayout.finishLoadMore();
                    }
                }, 2000);
            }
        });
        homeRestUsage.getTypeDetail(typeDetailId, searchString, style, cityName, sex, price, pager, Util.PAGE_SIZE);
    }


    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == typeDetailId) {
            if (msg.getObj() == null)
                return;
            TypeDetailListBean bean = (TypeDetailListBean) msg.getObj();
            if (pager == 1)
                list.clear();
            list.addAll(bean.getMyResult());
            adapter.setNewData(list);
        }
        if (taskId == industryListId) {
            if (msg.getData()) {
                allJobList = ((ResultBean) msg.getObj()).getIndustry();
                for (AllIndustryBean allIndustryBean : allJobList) {
                    if (style.equals(allIndustryBean.getTag())) {
                        bean = allIndustryBean;
                        return;
                    }
                }
            }
        }
    }

    @OnClick({R.id.boxCity, R.id.boxSex, R.id.boxOut, R.id.boxStyle})
    public void select(View view) {

        //判断软键盘是否打开，为true则关闭，否则不处理
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            Util.closeKeyboard(StyleSelectActivity.this);
        }
        switch (view.getId()) {
            case R.id.boxCity:
                SelectCityPopWindow mPopWindow = new SelectCityPopWindow(this, cityName);
                mPopWindow.setSelectCityListener(this);
                mPopWindow.showPopupWindow(boxStyle);
                break;
            case R.id.boxSex:
                SelectPopWindow popWindow = new SelectPopWindow(this);
                popWindow.setLitenner(this);
                popWindow.showPopupWindow(boxSex);
                break;
            case R.id.boxOut:
                SelectPriceWindow priceWindow = new SelectPriceWindow(this);
                priceWindow.setLitenner(this);
                priceWindow.showPopupWindow(boxOut);
                break;
            case R.id.boxStyle:
                SelectTypeWindow typeWindow = new SelectTypeWindow(this, bean);
                typeWindow.setLitenner(this);
                typeWindow.showPopupWindow(boxStyle);
                break;
        }
    }

    @Override
    public void isSelect(String str) {
        boxSex.setText(str);
        if ("男".equals(str))
            sex = "1";
        else if ("女".equals(str))
            sex = "0";
        else sex = "2";
        pager = 1;
        homeRestUsage.getTypeDetail(typeDetailId, searchString, style, cityName, sex, price, pager, Util.PAGE_SIZE);
    }

    @Override
    public void noticeSexCancel() {
        boxSex.setChecked(false);
    }

    @Override
    public void pickSelectCityValue(String value) {
        boxCity.setText(value);
        if ("不限".equals(value)) {
            cityName = "";
        } else
            cityName = value;
        pager = 1;
        homeRestUsage.getTypeDetail(typeDetailId, searchString, style, cityName, sex, price, pager, Util.PAGE_SIZE);
    }

    @Override
    public void selectCityCannel() {
        boxCity.setChecked(false);
    }

    @Override
    public void isPriceSelect(String str) {
        boxOut.setText(str);
        if ("不限".equals(str)) {
            price = "";
        } else if ("200以下".equals(str)) {
            price = "-200";
        } else if ("50000以上".equals(str)) {
            price = "50000-";
        } else {
            price = str;
        }
        pager = 1;
        homeRestUsage.getTypeDetail(typeDetailId, searchString, style, cityName, sex, price, pager, Util.PAGE_SIZE);
    }

    @Override
    public void noticePriceCancel() {
        boxOut.setChecked(false);
    }

    @Override
    public void isTypeSelect(String str) {
        boxStyle.setText(str);
        if ("不限".equals(str))
            style = getStyle;
        else style = str;
        pager = 1;
        homeRestUsage.getTypeDetail(typeDetailId, searchString, style, cityName, sex, price, pager, Util.PAGE_SIZE);
    }

    @Override
    public void noticeTypeCancel() {
        boxStyle.setChecked(false);
    }
}
